package com.mlethe.library.multi.pay.listener;

import com.mlethe.library.multi.pay.MultiMedia;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import org.json.JSONObject;

/**
 * 微信回调处理类
 *
 * @Author Mlethe
 * @Date 2023/3/15
 */
public class EventHandler implements IWXAPIEventHandler {

    private OnPayListener mOnPayListener;

    public EventHandler(OnPayListener listener) {
        this.mOnPayListener = listener;
    }

    @Override
    public void onReq(BaseReq baseReq) {
        switch (baseReq.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                break;
            default:
                break;
        }
    }

    @Override
    public void onResp(BaseResp baseResp) {
        int type = baseResp.getType();
        MultiMedia media = MultiMedia.WECHAT;
        try {
            int errCode = baseResp.errCode;
            if (type == ConstantsAPI.COMMAND_SENDAUTH) {
                // 授权登录
            } else if (type == ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX) {
                // 分享
            } else if (type == ConstantsAPI.COMMAND_PAY_BY_WX) {
                // 支付
                if (errCode == BaseResp.ErrCode.ERR_OK) {
                    // 支付成功
                    PayResp payResp = (PayResp) baseResp;
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("extData", payResp.extData);
                        jsonObject.put("openId", payResp.openId);
                        jsonObject.put("returnKey", payResp.returnKey);
                        jsonObject.put("prepayId", payResp.prepayId);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (mOnPayListener != null) {
                        mOnPayListener.onComplete(media, jsonObject);
                    }
                } else if (errCode == BaseResp.ErrCode.ERR_USER_CANCEL) {
                    // 支付取消
                    if (mOnPayListener != null) {
                        mOnPayListener.onCancel(media);
                    }
                } else if (errCode == BaseResp.ErrCode.ERR_AUTH_DENIED) {
                    // 权限验证失败
                    if (mOnPayListener != null) {
                        mOnPayListener.onFailure(media, errCode);
                    }
                } else if (errCode == BaseResp.ErrCode.ERR_SENT_FAILED) {
                    // 授权失败
                    if (mOnPayListener != null) {
                        mOnPayListener.onFailure(media, errCode);
                    }
                } else {
                    // 未知错误
                    if (mOnPayListener != null) {
                        mOnPayListener.onFailure(media, errCode);
                    }
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            if (mOnPayListener != null) {
                mOnPayListener.onDestroy();
            }
            mOnPayListener = null;
        }
    }
}
